/** 
* @author : 褚国强
* @date 创建时间：2021年6月24日 上午11:09:34 
* @version 1.0
*/
package com.bsoft.project.http;

import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

/**
 * @author : 褚国强
 * @date 创建时间：2021年6月24日 上午11:09:34
 * @Description:  http请求类
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class HttpUrlConnectionToInterface {

	/**
	 *
	 * 发送请求
	 * 
	 * @param url	    发送的url
	 * @param bodyMap    请求参数集合 key参数名 value为参数值
	 */
	public static String sendPost(String url, Map<String, String> bodyMap) throws Exception {
		String requestParams = JSON.toJSONString(bodyMap);
		CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(300 * 1000)
                .setConnectTimeout(300 * 1000)
                .build();
        HttpPost post = new HttpPost(url);
        post.setConfig(requestConfig);
        post.setHeader("Content-Type","application/json;charset=utf-8");
        StringEntity postingString = new StringEntity(requestParams,
                "utf-8");
        post.setEntity(postingString);
        HttpResponse response = httpClient.execute(post);
        String content = EntityUtils.toString(response.getEntity());
        return content;
	}
}
